package com.chaber.containers;

import java.io.InputStream;
import java.util.Properties;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.chaber.containers.views.MainView;

public class ProductManagementApp extends Application {

	private AnnotationConfigApplicationContext appContext;

	@Override
	public void start(Stage primaryStage) {
		try {
			String resourceName = "spring.profiles.active";
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			Properties props = new Properties();
			try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
			    props.load(resourceStream);
			}

			this.appContext = (AnnotationConfigApplicationContext) Initializator.initialize(props.getProperty("spring.profiles.active"));
			
			MainView mainView = this.appContext.getBean(MainView.class);
			Scene mainScene = new Scene(mainView);
			primaryStage.setScene(mainScene);
			
			primaryStage.setWidth(600);
			primaryStage.setHeight(600);
			primaryStage.setTitle("Product management by Kamil Chaber");
			primaryStage.show();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void stop() throws Exception {
		super.stop();
		((ConfigurableApplicationContext) this.appContext).close();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
